// 数据存储的工具箱
const StorageKey = 'mall'
export default {
  // 存储值
  setItem(key, value, modelName) {
    if (modelName) {
      let val = this.getItem(modelName)
      val[key] = value
      this.setItem(modelName, val)
    } else {
      let val = this.getStorage(key)
      val[key] = value
      window.sessionStorage.setItem(StorageKey, JSON.stringify(val))
    }
  },
  // 获取值
  getItem(key, modelName) {
    if (modelName) {
      let val = this.getItem(modelName)
      if (val) {
        return val[key]
      }
    }
    return this.getStorage()[key]
  },
  // 获取所有Storage
  getStorage() {
    return JSON.parse(window.sessionStorage.getItem(StorageKey) || '{}')
  },
  // 删除Storage
  clean(key, modelName) {
    let val = this.getStorage()
    if (modelName) {
      if (!val[modelName]) return
      delete val[modelName][key]
    } else {
      delete val[key]
    }
    window.sessionStorage.setItem(StorageKey, JSON.stringify(val))
  }
}
